<?php
ini_set('display_errors',1);

if($_GET['action'] == 'gene_model_track') {


#    $post = array('segment' => $param['chr_id'] . ':' . $param['start'] . ',' . $params['stop']);        
    $ch = curl_init();
    if(preg_match('/chr(\d+)/',$_GET['chr_name'],$m)){
        $chr = $m[1];
    } else {
        $chr = $_GET['chr_name'];
    }
    $url = $_GET['DASurl'] . $chr . ':' . $_GET['start'] . ',' . $_GET['stop'];
//    echo $url;
    curl_setopt($ch, CURLOPT_URL, $url);
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    
    $output = curl_exec($ch);
    curl_close($ch);
    $xml = simplexml_load_string($output);

// echo '<pre>';
//print_r($xml);    

    #print $xml->GFF->SEGMENT->FEATURE;
#    print_r($xml->GFF->SEGMENT->FEATURE);
    
#    $o = array();
    if(count($xml->GFF->SEGMENT->FEATURE) ==0) return;
    foreach($xml->GFF->SEGMENT->FEATURE as $feature) {
#        print_r($feature);
        $gene = $feature->GROUP[0]->attributes();
        #print_r($gene);
        //print $gene['id'];
        
        $o[(string)$gene['id']][] = array('gene_id' =>(string)$gene['id'],
                               'start' => (int)$feature->START[0],
                               'stop' => (int)$feature->END[0],
                               'strand' => (string)$feature->ORIENTATION[0],
                               'genename' => (string)$gene['label'],
                               'db_gene_id' => (string)$gene['id']
                            );
    }
   // print_r($o);
    echo json_encode($o);
}


?>